package biz.datalk.industrialland.admin.modules.common.controller;

import biz.datalk.industrialland.admin.modules.common.pojo.dto.CaptchaDTO;
import biz.datalk.industrialland.admin.modules.common.pojo.dto.CodeDTO;
import biz.datalk.industrialland.admin.modules.common.pojo.dto.LoginWithCodeDTO;
import biz.datalk.industrialland.admin.modules.common.pojo.vo.CaptchaVO;
import biz.datalk.industrialland.admin.modules.common.pojo.vo.GetUserInfoVO;
import biz.datalk.industrialland.admin.modules.common.pojo.vo.LoginWithCodeVO;
import biz.datalk.industrialland.admin.modules.common.servie.IAuthService;
import biz.datalk.industrialland.common.advice.CryptoDTO;
import biz.datalk.industrialland.common.aspect.annotation.ParamCheck;
import biz.datalk.industrialland.common.encrypt.annotation.RSAES;
import biz.datalk.industrialland.common.json.annotation.Json;
import biz.datalk.industrialland.common.log.aspect.annotation.SysLog;
import biz.datalk.industrialland.common.log.def.OptEnum;
import biz.datalk.industrialland.common.result.UnifyResult;
import biz.datalk.industrialland.common.validated.ChkSeq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author tarofang@163.com
 * @date 2024-01-15
 */
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    private final IAuthService authService;

    @Autowired
    public AuthController(IAuthService authService) {
        this.authService = authService;
    }

    /**
     * 获取图形验证码
     *
     * @param dto           入参
     * @param bindingResult 参数绑定结果
     *
     * @return 出参
     *
     * @author tarofang@163.com
     * @date 2023年09月05日
     */
    @ParamCheck
    @RSAES
    @PostMapping(value = "/captcha", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_LOGIN_10104)
    public CaptchaVO captcha(@RequestBody @Validated CaptchaDTO dto, BindingResult bindingResult) {
        return authService.captcha(dto);
    }

    /**
     * 获取手机验证码
     *
     * @param dto           入参
     * @param bindingResult 参数绑定结果
     *
     * @return 出参
     *
     * @author tarofang@163.com
     * @date 2023年09月05日
     */
    @ParamCheck
    @RSAES
    @PostMapping(value = "/code", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_LOGIN_10103)
    public UnifyResult code(@RequestBody @Validated(ChkSeq.class) CodeDTO dto, BindingResult bindingResult) {
        authService.code(dto);
        return UnifyResult.success();
    }

    /**
     * 登出
     *
     * @return 出参
     *
     * @author tarofang@163.com
     * @date 2023年09月15日
     */
    @ParamCheck
    @RSAES
    @PostMapping(value = "/login_out", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_LOGIN_10102)
    public UnifyResult loginOut() {
        authService.loginOut();
        return UnifyResult.success();
    }

    /**
     * 手机验证码登录
     *
     * @param dto           入参
     * @param bindingResult 参数绑定结果
     *
     * @return 出参
     *
     * @author tarofang@163.com
     * @date 2024年02月20日
     */
    @ParamCheck
    @RSAES
    @PostMapping(value = "/login_with_code", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_LOGIN_10105)
    public LoginWithCodeVO loginWithCode(@RequestBody @Validated(ChkSeq.class) LoginWithCodeDTO dto, BindingResult bindingResult) {
        return authService.loginWithCode(dto);
    }

    /**
     * 获取登录账号的用户信息
     *
     * @param bindingResult 参数绑定结果
     *
     * @return 出参
     *
     * @author tarofang@163.com
     * @date 2024年02月20日
     */
    @ParamCheck
    @RSAES
    @PostMapping(value = "/get_user_info", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_LOGIN_10106)
    @Json(type = GetUserInfoVO.class, filter = "sysUser")
    public GetUserInfoVO getUserInfo(@RequestBody @Validated CryptoDTO dto, BindingResult bindingResult) {
        return authService.getUserInfo();
    }

}
